$("#typeWay").change(function () { //多选框切换操作 下方城市框
    radiochecked();
});

function radiochecked(){
    var shipDCId = $('#typeWay option:selected').val();
    var dom_a = document.getElementById('allArea');
    if (shipDCId == 40) {
        dom_a.checked = false;
        $(dom_a).attr("disabled", false);
        dom_a.value = 0;
        $("#cityList").html('')
    }
    if (shipDCId == 20 || shipDCId == 30 || shipDCId == 10) {
        dom_a.checked = true;
        $(dom_a).attr("disabled", true);
        dom_a.value = 1;
        $($("#allArea")).val("1");
        $("#cityList").html('')
        $("#cityList").append('<li class="f-l ml-10" id="0"><span class="name">全国</span><sup style="color:#ff1b1b; cursor:pointer;">' +
            '<b onclick="remove(this)">x</b></sup></li>')
    }
}

$("#areaSelect").change(function () {
    amtWayChange();
});

function amtWayChange() { //页面加载 使发货仓没有的运送方式隐藏
    var warehouseCode = $('#areaSelect option:selected').val();
    var sel = document.getElementById("typeWay");
    $("#typeWay").html('');
    $.ajax({
        url: root.logistics.getDispatchModeByWarehousecode,
        //url: 'https://'+ window.location.host +'/api/admin/alogistics',
        cache: false, //禁用缓存
        type: "POST",
        dataType:"json",
        data:{warehouseCode:warehouseCode},
        success: function(obj){
            var typeWayList = obj.data;
            typeWayList = typeWayList.sort();
            for(var i=0;i<typeWayList.length;i++){
                if (typeWayList[i] == 10){
                    var option = new Option('快递','10');
                    sel.options.add(option)
                }
                if (typeWayList[i] == 20){
                    var option = new Option('快运','20');
                    sel.options.add(option)
                }
                if (typeWayList[i] == 30){
                    var option = new Option('专车包车','30');
                    sel.options.add(option)
                }
                if (typeWayList[i] == 40){
                    var option = new Option('鑫方盛物流','40');
                    sel.options.add(option)
                }
            }
            radiochecked();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}

function getAllDC(){
    var sel = document.getElementById("areaSelect"); //页面发货仓下拉框数据
    $.ajax({
        url: 'https://'+ window.location.host +'/api/admin/abaseservice/area/getAllWarehouse',
        cache: false, //禁用缓存
        type: "POST",
        dataType:"json",
        data:{},
        success: function(obj){
            for(var i=0;i<obj.data.length;i++){
                var option = new Option(obj.data[i].warehouse_name,obj.data[i].warehouse_code);
                sel.options.add(option)
            }
            amtWayChange();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}

$("#allArea").click(function () { //取消单选框选中效果
    var beforeClick = "";
    if(this.value==1){
        this.value=0;
        this.checked=0;
        $("#cityList").html('')

    }else{
        this.value=1;
        beforeClick = $("#cityList").children();
        $("#cityList").html('')
        $("#cityList").append('<li class="f-l ml-10" id="0"><span class="name">全国</span><sup style="color:#ff1b1b; cursor:pointer;">' +
            '<b onclick="remove(this)">x</b></sup></li>')
    }
})
function updateArae(thil){
    var dom_a = document.getElementById('allArea');
    if (dom_a.checked == false){
        $("#dizhi").modal("show");
    }else{
        layer.msg('默认全国无法修改', {icon: 5,time:1000});
    }
}

function saveCity(){
    var cityName= $("#vueProvinceName").text()+" "+$("#vueCityName").text()+" "+$("#vueCountyName").text()+" "+$("#vueVillageName").text();
    var goodId = "";
    if($("#provinceId").text() != null){
        goodId = $("#provinceId").text()
    }
    if($("#cityId").text() != ""){
        goodId = goodId +","+$("#cityId").text()
    }
    if($("#countyId").text() != ""){
        goodId = goodId +","+$("#countyId").text()
    }
    if($("#villageId").text() != ""){
        goodId = goodId +","+$("#villageId").text()
    }
    if(goodId == ""){
        layer.msg('未选择地区', {icon: 5,time:1000});
        return;
    }

    var ulAll = $("#cityList").children();
    var idOne = goodId.split(",");
    if (ulAll.length == 1){
        if (ulAll[0].id == 0){
            $("#ulList").html("")
        }
    }
    for(var i=0;i<ulAll.length;i++){
        if(ulAll[i].id == idOne[idOne.length-1]){ //idOne[idOne.length-1]
            layer.msg('选择范围重复', {icon: 5,time:1000});
            return;
        }
    }

    $("#cityList").append('<li class="f-l ml-10" id="'+goodId+'"><span class="name">'+cityName+'</span><sup style="color:#ff1b1b; cursor:pointer;">' +
        '<b onclick="remove(this)">x</b></sup></li>')
    $("#dizhi").modal("hide");
}

function remove(thil){
    var shipDCId = $('#typeWay option:selected').val();
    if(shipDCId !=40){
        layer.msg('默认全国无法修改', {icon: 5,time:2000});
    }else {
        if ($(thil).parent().parent()[0].id == 0){
            $("input[type='radio'][name='allArea']").attr("checked", false)
        }
        $(thil).parent().parent().remove();
    }
}

$("#cancel").click(function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
})

$("#saveNew").click(function () {
    var loginData=JSON.parse(web.readCookie("loginData"));
    var cityList = document.getElementById('cityList');

    var shipDCId = $('#areaSelect option:selected').val();
    //var shipDCName = $('#areaSelect option:selected').text();
    var wayTypeId = $('#typeWay option:selected').val();
    //var wayTypeName = $('#typeWay option:selected').text();
    var transportType;
    if(wayTypeId == 40){
        transportType = 10;
    }else{
        transportType = 20;
    }

    var transportModeType = $('#typeWay option:selected').val()

    var cityidLast = "";
    var cityNameLast = "";
    var liList = $(cityList).children();
    for(var i=0;i<liList.length;i++){
        var cityid = liList[i].id.split(",");
        cityidLast = cityid[cityid.length-1]+','+cityidLast;
        cityNameLast = $(liList[i]).find('.name').text()+','+cityNameLast;
    }
    var createUser = loginData.user.name; //创建人

    $.ajax({
        url: 'https://'+ window.location.host +'/api/admin/alogistics/addDispatchScope.jhtml',
        cache: false, //禁用缓存
        type: "POST",
        data: {
            warehouseCode: shipDCId,
            transportType: transportType,
            transportModeType: transportModeType,
            areaCode: cityidLast,
            areaName: cityNameLast,
            createUser: createUser,
            status:10
        },
        success: function(data){
            if(data.sub_code == 0) {
                layer.msg(data.sub_msg, {
                    icon: 1,
                    time: 1000,
                    end: function () {
                        var index = parent.location.reload();
                        parent.layer.close(index);
                    }
                });
            }else{
                layer.msg(data.sub_msg, {icon: 5,time:2000});
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest);
            //alert(XMLHttpRequest);
        }
    });
})

